﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DMS.Integration;

namespace Demo
{
    public class Program
    {
        static void Main(string[] args)
        {
            //ReceiptTest();
            InvoiceTest();
        }
        static void ReceiptTest()
        {
            SharePointHelper helper = new SharePointHelper();


            //upload a pdf document to sharepoint
            UploadReceiptDoc(helper);
            //insert a metadata of invoice
            UpdateReceiptMetadata(helper);
            //retrieve document url from sharepoint 
            GetReceiptDocUrl(helper);
        }

        private static void GetReceiptDocUrl(SharePointHelper helper)
        {
            string result = helper.GetDocumentUrlById("20120131-1250", DocumentType.Receipt);
            Console.WriteLine(result);

            Console.WriteLine("Done!");
            Console.ReadLine();
        }

        private static void UpdateReceiptMetadata(SharePointHelper helper)
        {
            ReceiptDocMetadata metadata = new ReceiptDocMetadata
                                              {
                                                  Year = 2011,
                                                  Id = "20120131-1250",
                                                  BranchName = helper.GetBranchLookupDataByName("Auckland Commercial"),
                                                  CustomerName = "Steven Wu",
                                                  CustomerNumber = "1234",
                                                  Amount = 123.00M,
                                                  BidderNo = "9999",
                                                  FirstName = "YC"
                                              };
            bool success = helper.UpdateMetadata(DocumentType.Receipt, metadata);
            if(success)
                Console.WriteLine("metadata saved.");
            else
            {
                Console.WriteLine("save failed.");
            }
            Console.ReadLine();
        }

        private static void UploadReceiptDoc(SharePointHelper helper)
        {
            string filename = "20120131-1250.pdf";
            byte[] contents = SharePointHelper.GetFileFromFileSystem(@"D:\Document Types\Receipts\20120131-1250.pdf");
            int id = helper.UploadFile(DocumentType.Receipt, 2011, filename, contents);
            Console.WriteLine("document uploaded.");
            Console.ReadLine();
        }

        static void InvoiceTest()
        {
            SharePointHelper helper = new SharePointHelper();


            //upload a pdf document to sharepoint
            UploadInvoiceDoc(helper);
            //insert a metadata of invoice
            UpdateInvoiceMetadata(helper);
            //retrieve document url from sharepoint 
            GetInvoiceDocUrl(helper);
        }

        private static void GetInvoiceDocUrl(SharePointHelper helper)
        {
            string result = helper.GetDocumentUrlById("21992-2", DocumentType.Invoice);
            Console.WriteLine(result);

            Console.WriteLine("Done!");
            Console.ReadLine();
        }

        private static void UpdateInvoiceMetadata(SharePointHelper helper)
        {
            InvoiceDocMetadata metadata = new InvoiceDocMetadata
                                              {
                                                  Year = 2011,
                                                  InvoiceGUID = "abdeb71f-b556-4a46-a464-c275f06700f8",
                                                  Id = "21992-2",
                                                  InvoiceType = "BINV",
                                                  CustomerNumber = "238705",
                                                  DocumentType = "Invoice",
                                                  InvoiceDate = new DateTime(2012, 1, 19, 23, 55, 12),
                                                  InvoiceTotal = 13.37M,
                                                  CustomerName = "Robyn Porima",
                                                  BranchName = helper.GetBranchLookupDataByCode(503),
                                                  GoodGUID = "b0459448-61e2-422a-b8ea-c6254f33afb0",
                                                  AuctionID = "22847",
                                                  InvoiceNumberSuffix = "2",
                                                  GoodID = "975713",
                                                  ConsignmentVendorNumber = "623278",
                                                  ConsignmentGoodNumber = "73",
                                                  PlateNumber = "ERP805",
                                                  VIN = "12334567890"
                                              };
            helper.UpdateMetadata(DocumentType.Invoice, metadata);
            Console.WriteLine("metadata saved.");
            Console.ReadLine();
        }

        private static void UploadInvoiceDoc(SharePointHelper helper)
        {
            string filename = "21992-2.pdf";
            byte[] contents = SharePointHelper.GetFileFromFileSystem(@"D:\Document Types\Invoices\21992-2.pdf");
            int id = helper.UploadFile(DocumentType.Invoice, 2011, filename, contents);
            Console.WriteLine("document uploaded.");
            Console.ReadLine();
        }
    }
}
